// Hydrant Jet Fix 1.0 for GTA San Andreas (PC 1.0 US) - by Aaron PV
// CLEO script source. Compile with Sanny Builder (SA mode) to HydrantJetFix.cs.
//
// Broken fire hydrants no longer shoot their water jet straight through cars.
// When a car is over a jet: the jet stops at the car, water sprays out in a ring
// and ripples under the car, and the water pushes up the part of the car it hits.
//
// How it works: the game's hydrant jet function (0x4A0D70) is redirected in memory
// to a tiny stub that records where hydrants break; this script then draws the jet
// itself. Nothing on disk is changed. Delete the .cs to remove the mod.
//
// Settings (edit the numbers marked with these names, then recompile):
//   JET_TIME        25000   how long a jet lasts (ms)
//   RANGE           50.0    jets further from the player than this are not drawn (m)
//   HALF_WIDTH      1.0     half the car width used to test "jet is under the car" (m)
//   HALF_LENGTH     2.3     half the car length used for the same test (m)
//   LIFT_MAX        0.6     the push stops above this height over the jet (m)
//   LIFT_STRENGTH   0.0025  push strength
//   LIFT_SPEED_CAP  0.012   maximum upward speed from the push
//   RIPPLE_GAP      1500    a new ripple replaces the old one this often (ms)
//   SPLASH_GAP      250     time between spray bursts (ms)
//   MUFFLE_DEPTH    8.0     depth of the hidden jet used for the water sound under a car (m)
//
// Opcode lines are written in the game's parameter order (results last).
// Only failures are logged, to HydrantJetFix.log in the game folder.

{$CLEO .cs}
0000: NOP
03A4: 'HYDRFIX'

:START

// ---- check the expected 1.0 US code ----
0A8D: 0x4A0D79 4 1 6@

if
  6@ == -349126525
jf @FAIL_EXE

0@ = 0
0A8D: 0x4A0D70 1 1 6@

if
  6@ == 0xE9
jf @CHECK_A1
0A8D: 0x4A0D71 4 1 29@
29@ += 0x4A0D75

if
  29@ > 0x400FFF
jf @FOREIGN_HOOK
if
  0x1577000 > 29@
jf @FOREIGN_HOOK
jump @INSTALL

:FOREIGN_HOOK
29@ -= 16
0A8D: 29@ 4 0 6@
if
  6@ == 0x52445948
jf @FAIL_HOOK
0085: 0@ 29@

jump @READY

:CHECK_A1
if
  6@ == 0xA1
jf @FAIL_EXE

:INSTALL
if
0AA2: "kernel32.dll" 27@
jf @FAIL_API
if
0AA4: "VirtualAlloc" 27@ 30@
jf @FAIL_API
// VirtualAlloc(NULL, 4096, 0x3000, 0x40): arguments written last-first
0AA7: 30@ 4 0 0x40 0x3000 4096 0 0@

if
  0@ == 0
jf @WRITE_STUB
jump @FAIL_API

:WRITE_STUB
0A8C: 0@ 4 0x52445948 0
0085: 6@ 0@
6@ += 16
0A8C: 6@ 4 69485707 0
0085: 6@ 0@
6@ += 20
0A8C: 6@ 2 5515 0
0085: 6@ 0@
6@ += 22
0085: 29@ 0@
29@ += 256
0A8C: 6@ 4 29@ 0
0085: 6@ 0@
6@ += 26
0A8C: 6@ 2 57987 0
0085: 6@ 0@
6@ += 28
0A8C: 6@ 1 15 0
0085: 6@ 0@
6@ += 29
0A8C: 6@ 2 5261 0
0085: 6@ 0@
6@ += 31
0A8C: 6@ 1 82 0
0085: 6@ 0@
6@ += 32
0A8C: 6@ 2 58049 0
0085: 6@ 0@
6@ += 34
0A8C: 6@ 1 2 0
0085: 6@ 0@
6@ += 35
0A8C: 6@ 2 2187 0
0085: 6@ 0@
6@ += 37
0A8C: 6@ 2 35465 0
0085: 6@ 0@
6@ += 39
0085: 29@ 0@
29@ += 260
0A8C: 6@ 4 29@ 0
0085: 6@ 0@
6@ += 43
0A8C: 6@ 2 18571 0
0085: 6@ 0@
6@ += 45
0A8C: 6@ 1 4 0
0085: 6@ 0@
6@ += 46
0A8C: 6@ 2 35465 0
0085: 6@ 0@
6@ += 48
0085: 29@ 0@
29@ += 264
0A8C: 6@ 4 29@ 0
0085: 6@ 0@
6@ += 52
0A8C: 6@ 2 18571 0
0085: 6@ 0@
6@ += 54
0A8C: 6@ 1 8 0
0085: 6@ 0@
6@ += 55
0A8C: 6@ 2 35465 0
0085: 6@ 0@
6@ += 57
0085: 29@ 0@
29@ += 268
0A8C: 6@ 4 29@ 0
0085: 6@ 0@
6@ += 61
0A8C: 6@ 2 1535 0
0085: 6@ 0@
6@ += 63
0085: 29@ 0@
29@ += 256
0A8C: 6@ 4 29@ 0
0085: 6@ 0@
6@ += 67
0A8C: 6@ 2 1218 0
0085: 6@ 0@
6@ += 69
0A8C: 6@ 1 0 0
// verify the code bytes before enabling the jump
0085: 6@ 0@
6@ += 16
0A8D: 6@ 4 0 29@
if
  29@ == 69485707
jf @BAD_STUB
0085: 6@ 0@
6@ += 20
0A8D: 6@ 2 0 29@
if
  29@ == 5515
jf @BAD_STUB
0085: 6@ 0@
6@ += 26
0A8D: 6@ 2 0 29@
if
  29@ == 57987
jf @BAD_STUB
0085: 6@ 0@
6@ += 28
0A8D: 6@ 1 0 29@
if
  29@ == 15
jf @BAD_STUB
0085: 6@ 0@
6@ += 29
0A8D: 6@ 2 0 29@
if
  29@ == 5261
jf @BAD_STUB
0085: 6@ 0@
6@ += 31
0A8D: 6@ 1 0 29@
if
  29@ == 82
jf @BAD_STUB
0085: 6@ 0@
6@ += 32
0A8D: 6@ 2 0 29@
if
  29@ == 58049
jf @BAD_STUB
0085: 6@ 0@
6@ += 34
0A8D: 6@ 1 0 29@
if
  29@ == 2
jf @BAD_STUB
0085: 6@ 0@
6@ += 35
0A8D: 6@ 2 0 29@
if
  29@ == 2187
jf @BAD_STUB
0085: 6@ 0@
6@ += 37
0A8D: 6@ 2 0 29@
if
  29@ == 35465
jf @BAD_STUB
0085: 6@ 0@
6@ += 43
0A8D: 6@ 2 0 29@
if
  29@ == 18571
jf @BAD_STUB
0085: 6@ 0@
6@ += 45
0A8D: 6@ 1 0 29@
if
  29@ == 4
jf @BAD_STUB
0085: 6@ 0@
6@ += 46
0A8D: 6@ 2 0 29@
if
  29@ == 35465
jf @BAD_STUB
0085: 6@ 0@
6@ += 52
0A8D: 6@ 2 0 29@
if
  29@ == 18571
jf @BAD_STUB
0085: 6@ 0@
6@ += 54
0A8D: 6@ 1 0 29@
if
  29@ == 8
jf @BAD_STUB
0085: 6@ 0@
6@ += 55
0A8D: 6@ 2 0 29@
if
  29@ == 35465
jf @BAD_STUB
0085: 6@ 0@
6@ += 61
0A8D: 6@ 2 0 29@
if
  29@ == 1535
jf @BAD_STUB
0085: 6@ 0@
6@ += 67
0A8D: 6@ 2 0 29@
if
  29@ == 1218
jf @BAD_STUB
0085: 6@ 0@
6@ += 69
0A8D: 6@ 1 0 29@
if
  29@ == 0
jf @BAD_STUB

0085: 29@ 0@
29@ += 16
29@ -= 0x4A0D75
0A8C: 0x4A0D71 4 29@ 1
0A8C: 0x4A0D70 1 0xE9 1
0A8D: 0x4A0D70 1 1 6@
0A8D: 0x4A0D71 4 1 29@


:READY
0085: 6@ 0@
6@ += 0x100
0A8D: 6@ 4 0 1@

8@ = 0
9@ = 0
10@ = 0
14@ = 0
15@ = 0
16@ = 0
28@ = 0
32@ = 0
// clear ripple handles/times (page+0x200 .. +0x21F)
0085: 6@ 0@
6@ += 0x200
0A8C: 6@ 4 0 0
6@ += 4
0A8C: 6@ 4 0 0
6@ += 4
0A8C: 6@ 4 0 0
6@ += 8
0A8C: 6@ 4 0 0
6@ += 4
0A8C: 6@ 4 0 0
6@ += 4
0A8C: 6@ 4 0 0
// clear hidden-jet handles (page+0x250 .. +0x258)
0085: 6@ 0@
6@ += 0x250
0A8C: 6@ 4 0 0
6@ += 4
0A8C: 6@ 4 0 0
6@ += 4
0A8C: 6@ 4 0 0

:LOOP
0001: 0
// ---- new jets from the game (counter written by the hook) ----
0085: 6@ 0@
6@ += 0x100
0A8D: 6@ 4 0 2@
:NO_TICK
if
0256: 0
jf @LOOP
01BD: 26@
0085: 5@ 2@
0062: 5@ 1@
if
  5@ > 16
jf @QUEUE
0085: 1@ 2@
1@ -= 16

:QUEUE
0085: 5@ 2@
0062: 5@ 1@
if
  0 > 5@
jf @QUEUE_OK
0085: 1@ 2@                    // read counter ran ahead: resync
jump @SLOTS_START
:QUEUE_OK
if
  5@ > 0
jf @SLOTS_START
// slot = 1@ mod 16 (no bitwise opcode)
0085: 6@ 1@
:MOD16
if
  6@ > 15
jf @MOD_DONE
6@ -= 16
jump @MOD16
:MOD_DONE
if
  6@ > -1
jf @QUEUE_BAD
if
  16 > 6@
jf @QUEUE_BAD
6@ *= 12
6@ += 0x104
005A: 6@ 0@
0A8D: 6@ 4 0 3@
6@ += 4
0A8D: 6@ 4 0 4@
6@ += 4
0A8D: 6@ 4 0 5@
1@ += 1

gosub @ADD_JET
jump @QUEUE

:QUEUE_BAD
if
0A9A: "root:\HydrantJetFix.log" "at" 31@
jf @L0
0AD9: 31@ "FAIL: bad queue slot %d, resyncing%c" 6@ 10
0A9B: 31@
:L0
0085: 1@ 2@

:SLOTS_START
7@ = 0
:SLOTS
if
  7@ > 2
jf @SLOT_BODY
jump @LOOP

:SLOT_BODY
if
  14@(7@,3i) == 0
jf @SLOT_ACTIVE
jump @SLOT_NEXT

:SLOT_ACTIVE
0085: 6@ 26@
0062: 6@ 11@(7@,3i)
if
  6@ > 25000                  // JET_TIME
jf @SLOT_CAR
0650: 8@(7@,3i)
14@(7@,3i) = 0
gosub @KILL_RIPPLE
gosub @KILL_MUFFLE

jump @SLOT_NEXT

:SLOT_CAR
if
0AE2: 17@(7@,3f) 20@(7@,3f) 23@(7@,3f) 3.5 0 1 27@
jf @SLOT_CLEAR
// car matrix: right +0x00, forward +0x10, position +0x30
0A97: 27@ 30@
0085: 5@ 30@
5@ += 0x14
0A8D: 5@ 4 0 5@                // 5@ = matrix pointer
0085: 29@ 5@
29@ += 0x30
0A8D: 29@ 4 0 3@               // car x
29@ += 4
0A8D: 29@ 4 0 4@               // car y
29@ += 4
0A8D: 29@ 4 0 6@               // car z
0063: 6@ 23@(7@,3f)            // 6@ = car height above jet base
if and
  6@ > -1.0
  4.0 > 6@
jf @SLOT_CLEAR
// dx, dy from car centre to jet (world)
0087: 29@ 17@(7@,3f)
0063: 29@ 3@
0085: 3@ 29@                   // 3@ = dx
0087: 29@ 20@(7@,3f)
0063: 29@ 4@
0085: 4@ 29@                   // 4@ = dy
// localX = dx*right.x + dy*right.y
0A8D: 5@ 4 0 29@
006B: 29@ 3@
0085: 2@ 5@
2@ += 4
0A8D: 2@ 4 0 2@
006B: 2@ 4@
005B: 29@ 2@                   // 29@ = localX
// localY = dx*fwd.x + dy*fwd.y
0085: 2@ 5@
2@ += 0x10
0A8D: 2@ 4 0 2@
006B: 2@ 3@
0085: 31@ 5@
31@ += 0x14
0A8D: 31@ 4 0 31@
006B: 31@ 4@
005B: 2@ 31@                   // 2@ = localY
// is the jet under the car body? (approximate footprint)
if
  29@ > 1.0                    // HALF_WIDTH
jf @FP1
jump @SLOT_CLEAR
:FP1
if
  -1.0 > 29@
jf @FP2
jump @SLOT_CLEAR
:FP2
if
  2@ > 2.3                     // HALF_LENGTH
jf @FP3
jump @SLOT_CLEAR
:FP3
if
  -2.3 > 2@
jf @FP_OK
jump @SLOT_CLEAR
:FP_OK
// save localX, localY, local ground z, dx, dy to scratch (page+0x230..)
0085: 31@ 0@
31@ += 0x230
0A8C: 31@ 4 29@ 0
31@ += 4
0A8C: 31@ 4 2@ 0
31@ += 4
29@ = 0.0
0063: 29@ 6@
29@ -= 0.35                    // ground below jet base, relative to car centre
0A8C: 31@ 4 29@ 0
31@ += 4
0A8C: 31@ 4 3@ 0
31@ += 4
0A8C: 31@ 4 4@ 0

if
  14@(7@,3i) == 1
jf @LIFT
064E: 8@(7@,3i)
14@(7@,3i) = 2
// muffled water sound while covered: a hidden second jet underground
gosub @MUFFLE_ADDR
0087: 5@ 23@(7@,3f)
5@ -= 8.0                      // MUFFLE_DEPTH (m below the jet base)
064B: "water_hydrant" 17@(7@,3f) 20@(7@,3f) 5@ 0 4@
064C: 4@
0A8C: 3@ 4 4@ 0


// ---- lift: force at the point where the water hits (tilts the car) ----
:LIFT
if
  0.6 > 6@                     // LIFT_MAX height above jet base
jf @RIPPLE
0A97: 27@ 30@
0085: 29@ 30@
29@ += 0x4C
0A8D: 29@ 4 0 29@              // current upward speed
if
  0.012 > 29@                  // LIFT_SPEED_CAP
jf @RIPPLE
0085: 5@ 30@
5@ += 0x8C
0A8D: 5@ 4 0 5@                // mass
5@ *= 0.0025                   // LIFT_STRENGTH (speed added per frame at the hit point)
0085: 31@ 0@
31@ += 0x238
0A8D: 31@ 4 0 29@              // point z
31@ += 4
0A8D: 31@ 4 0 3@               // point x (dx)
31@ += 4
0A8D: 31@ 4 0 4@               // point y (dy)
// CPhysical::ApplyForce(force xyz, point xyz, applyTurn) at 0x542B50; args written last-first
0AA6: 0x542B50 30@ 7 0 1 29@ 4@ 3@ 5@ 0.0 0.0

// ---- ripple: one per jet, replaced every RIPPLE_GAP ----
:RIPPLE
gosub @RIPPLE_ADDR             // 3@ = handle address, 4@ = time address
0A8D: 4@ 4 0 5@
0085: 6@ 26@
0062: 6@ 5@
if
  6@ > 1500                    // RIPPLE_GAP ms
jf @SPRAY_TIMER
0A8D: 3@ 4 0 5@
if
  5@ == 0
jf @RIPPLE_OLD
jump @RIPPLE_NEW
:RIPPLE_OLD
0650: 5@
:RIPPLE_NEW
0087: 31@ 23@(7@,3f)
31@ -= 0.35
064B: "water_ripples" 17@(7@,3f) 20@(7@,3f) 31@ 4 5@
064C: 5@
0A8C: 3@ 4 5@ 0
0A8C: 4@ 4 26@ 0

// ---- spray: small splash at the hit point + ring of sideways splashes ----
:SPRAY_TIMER
0085: 6@ 26@
0062: 6@ 28@
if
  6@ > 0
jf @SLOT_NEXT
0085: 28@ 26@
28@ += 250                     // SPLASH_GAP
0087: 29@ 23@(7@,3f)
29@ -= 0.35
064B: "water_splsh_sml" 17@(7@,3f) 20@(7@,3f) 29@ 4 30@
064F: 30@
// load localX -> 2@, localY -> 5@, local ground z -> 6@
0085: 31@ 0@
31@ += 0x230
0A8D: 31@ 4 0 2@
31@ += 4
0A8D: 31@ 4 0 5@
31@ += 4
0A8D: 31@ 4 0 6@
// ring point 1/6
0087: 3@ 2@
3@ += 0.500
0087: 4@ 5@
4@ += 0.000
066C: "water_splash" 27@ 3@ 4@ 6@ 1.0 0.0 0.25 4 30@   // name car offset(xyz) direction(xyz) type -> handle
064F: 30@
// ring point 2/6
0087: 3@ 2@
3@ += 0.250
0087: 4@ 5@
4@ += 0.433
066C: "water_splash" 27@ 3@ 4@ 6@ 0.5 0.866 0.25 4 30@   // name car offset(xyz) direction(xyz) type -> handle
064F: 30@
// ring point 3/6
0087: 3@ 2@
3@ += -0.250
0087: 4@ 5@
4@ += 0.433
066C: "water_splash" 27@ 3@ 4@ 6@ -0.5 0.866 0.25 4 30@   // name car offset(xyz) direction(xyz) type -> handle
064F: 30@
// ring point 4/6
0087: 3@ 2@
3@ += -0.500
0087: 4@ 5@
4@ += 0.000
066C: "water_splash" 27@ 3@ 4@ 6@ -1.0 0.0 0.25 4 30@   // name car offset(xyz) direction(xyz) type -> handle
064F: 30@
// ring point 5/6
0087: 3@ 2@
3@ += -0.250
0087: 4@ 5@
4@ += -0.433
066C: "water_splash" 27@ 3@ 4@ 6@ -0.5 -0.866 0.25 4 30@   // name car offset(xyz) direction(xyz) type -> handle
064F: 30@
// ring point 6/6
0087: 3@ 2@
3@ += 0.250
0087: 4@ 5@
4@ += -0.433
066C: "water_splash" 27@ 3@ 4@ 6@ 0.5 -0.866 0.25 4 30@   // name car offset(xyz) direction(xyz) type -> handle
064F: 30@
jump @SLOT_NEXT

:SLOT_CLEAR
if
  14@(7@,3i) == 2
jf @SLOT_NEXT
// recreate the jet: a stopped effect does not restart its water sound
0650: 8@(7@,3i)
064B: "water_hydrant" 17@(7@,3f) 20@(7@,3f) 23@(7@,3f) 0 8@(7@,3i)
064C: 8@(7@,3i)
14@(7@,3i) = 1
gosub @KILL_RIPPLE
gosub @KILL_MUFFLE


:SLOT_NEXT
7@ += 1
jump @SLOTS

:RIPPLE_ADDR
0085: 3@ 7@
3@ *= 4
3@ += 0x200
005A: 3@ 0@
0085: 4@ 3@
4@ += 0x10
0051:

:MUFFLE_ADDR
0085: 3@ 7@
3@ *= 4
3@ += 0x250
005A: 3@ 0@
0051:

:KILL_MUFFLE
gosub @MUFFLE_ADDR
0A8D: 3@ 4 0 5@
if
  5@ == 0
jf @KILL_MUFFLE_IT
0051:
:KILL_MUFFLE_IT
0650: 5@
0A8C: 3@ 4 0 0
0051:

:KILL_RIPPLE
gosub @RIPPLE_ADDR
0A8D: 3@ 4 0 5@
if
  5@ == 0
jf @KILL_IT
0051:
:KILL_IT
0650: 5@
0A8C: 3@ 4 0 0
0A8C: 4@ 4 0 0
0051:

:ADD_JET
01F5: 0 6@
00A0: 6@ 27@ 29@ 30@
050A: 3@ 4@ 5@ 27@ 29@ 30@ 6@
if
  6@ > 50.0                   // RANGE
jf @ADD_FIND

0051:

:ADD_FIND
7@ = 0
:ADD_LOOP
if
  14@(7@,3i) == 0
jf @ADD_NEXT
064B: "water_hydrant" 3@ 4@ 5@ 0 8@(7@,3i)   // type 0 = same as the game (keeps the water sound)
064C: 8@(7@,3i)
0085: 11@(7@,3i) 26@
14@(7@,3i) = 1
0087: 17@(7@,3f) 3@
0087: 20@(7@,3f) 4@
0087: 23@(7@,3f) 5@

0051:
:ADD_NEXT
7@ += 1
if
  7@ > 2
jf @ADD_LOOP

0051:

:BAD_STUB
if
0A9A: "root:\HydrantJetFix.log" "at" 31@
jf @L1
0AD9: 31@ "FAIL: stub byte check failed at 0x%08X, read 0x%08X; hook NOT enabled%c" 6@ 29@ 10
0A9B: 31@
:L1
jump @FAIL
:FAIL_EXE
if
0A9A: "root:\HydrantJetFix.log" "at" 31@
jf @L2
0AD9: 31@ "FAIL: unexpected exe code; nothing changed%c" 10
0A9B: 31@
:L2
jump @FAIL
:FAIL_HOOK
if
0A9A: "root:\HydrantJetFix.log" "at" 31@
jf @L3
0AD9: 31@ "FAIL: another mod already hooks 0x4A0D70; nothing changed%c" 10
0A9B: 31@
:L3
jump @FAIL
:FAIL_API
if
0A9A: "root:\HydrantJetFix.log" "at" 31@
jf @L4
0AD9: 31@ "FAIL: could not get memory from VirtualAlloc; nothing changed%c" 10
0A9B: 31@
:L4
:FAIL
0ACD: "Hydrant Jet Fix: not installed, see HydrantJetFix.log" 4000
0A93:
